﻿@page "/lessons/mainplan/{MainPlanId}"
@inject IMainPlanService MainPlanService
@inject ILessonService LessonService
@inject IJSRuntime JsRuntime
<PageTitle>主计划经验教训列表</PageTitle>
<div class="row position-relative">
    <div class="col">
        <h5>
            <CompMainPlanStatus Status="@_mainPlanDto.Status"></CompMainPlanStatus>
            <span> @_mainPlanDto.Number @_mainPlanDto.Name</span>

            @if (_mainPlanDto.Status==MainPlanStatus_e.结束)
            {
                <AuthorizeView Roles="admin,mgr,pm">
                    <Authorized>
                        <button class="btn btn-outline-success ms-3" @onclick="OpenPlan">重新打开项目</button>
                    </Authorized>
                </AuthorizeView>
                <span style="color: dodgerblue" class="ms-3">
                    结束日期：@(_mainPlanDto.ClosedTime != null ?
                   _mainPlanDto.ClosedTime.Value.ToString("MM/dd") : "")
                </span>
            }
            else if (_mainPlanDto.Status==MainPlanStatus_e.发货 && _mainPlanDto.AllIssueClosed)
            {
                <AuthorizeView Roles="admin,mgr,pm">
                    <Authorized>
                        <button class="btn btn-outline-danger ms-3" @onclick="ClosePlan">结束项目</button>
                    </Authorized>
                </AuthorizeView>
                <CompAddButton Roles="" Url="@($"lesson/add/{MainPlanId}")" />
            }
            else if (_mainPlanDto.Status == MainPlanStatus_e.发货 && !_mainPlanDto.AllIssueClosed)
            {
                <span style="color: red">
                    （不能关闭项目，存在异常：
                    <a href="issues/mainplan/@_mainPlanDto.Id">
                        <span>@_mainPlanDto.IssueTitles</span>
                        <img class="ms-1 me-2" style="width: 23px" src="andon_red.gif" />
                    </a>
                    ）
                </span>
                <CompAddButton Roles="" Url="@($"lesson/add/{MainPlanId}")" />
            }
            else
            {
                <CompAddButton Roles="" Url="@($"lesson/add/{MainPlanId}")" />
            }
        </h5>
    </div>

</div>
@if (_mainPlanDto.Id != null)
{
    if (_lessonDtos.Count == 0)
    {
        <span>未添加经验教训</span>
    }
    else
    {
        @foreach (var lesson in _lessonDtos)
        {
            <LessonRow LessonDto="lesson" />
        }
    }
}


@code {
    [Parameter]
    public string MainPlanId { get; set; }
    private MainPlanDto _mainPlanDto = new();
    private List<LessonDto> _lessonDtos = new();

    protected override async Task OnParametersSetAsync()
    {
        var mResult = await MainPlanService.GetFirstOrDefaultAsync(Guid.Parse(MainPlanId));
        if (mResult.Status) _mainPlanDto = mResult.Result;

        _lessonDtos.Clear();
        _lessonDtos.AddRange(await LessonService.GetAllByMainPlanIdAsync(_mainPlanDto.Id.Value));
    }

    private async void OpenPlan()
    {
        _mainPlanDto.ClosedTime = null;
        _mainPlanDto.Status = MainPlanStatus_e.发货;

        var result = await MainPlanService.UpdateStatusesAsync(_mainPlanDto.Id.Value, _mainPlanDto);
        if (result.IsSuccessStatusCode)
        {
            await JsRuntime.InvokeVoidAsync("alert", "已重新打开项目！");
        }
        StateHasChanged();
    }

    private async void ClosePlan()
    {
        _mainPlanDto.ClosedTime = DateTime.Now;
        _mainPlanDto.Status = MainPlanStatus_e.结束;
        var result = await MainPlanService.UpdateStatusesAsync(_mainPlanDto.Id.Value, _mainPlanDto);
        if (result.IsSuccessStatusCode)
        {
            await JsRuntime.InvokeVoidAsync("alert", "关闭项目成功！");
        }
        StateHasChanged();
    }


}

